import React from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import { ThemeProvider, createTheme, CssBaseline } from '@mui/material';
import { AuthProvider } from './contexts/AuthContext';
import { ModelProvider } from './contexts/ModelContext';
import ProtectedRoute from './components/ProtectedRoute';

// 页面
import Login from './pages/Login';
import Register from './pages/Register';
import Dashboard from './pages/Dashboard';
import ModelList from './pages/ModelList';
import ModelCreate from './pages/ModelCreate';
import ModelEdit from './pages/ModelEdit';
import ModelFiles from './pages/ModelFiles';
import ModelView from './pages/ModelView';
import GeometryModelCreate from './pages/GeometryModelCreate';
import GeometryModelEdit from './pages/GeometryModelEdit';
import MaterialLibrary from './components/MaterialLibrary';

// 创建主题
const theme = createTheme({
  palette: {
    primary: {
      main: '#2196f3',
      light: '#4dabf5',
      dark: '#1769aa',
      contrastText: '#fff',
    },
    secondary: {
      main: '#f50057',
      light: '#f73378',
      dark: '#ab003c',
      contrastText: '#fff',
    },
    background: {
      default: '#f5f5f5',
    },
  },
  typography: {
    fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif',
    h1: {
      fontWeight: 700,
    },
    h2: {
      fontWeight: 700,
    },
    h3: {
      fontWeight: 600,
    },
    h4: {
      fontWeight: 600,
    },
    h5: {
      fontWeight: 500,
    },
    h6: {
      fontWeight: 500,
    },
  },
  shape: {
    borderRadius: 8,
  },
  components: {
    MuiButton: {
      styleOverrides: {
        root: {
          textTransform: 'none',
          borderRadius: 8,
        },
      },
    },
    MuiPaper: {
      styleOverrides: {
        root: {
          borderRadius: 8,
        },
      },
    },
  },
});

const App = () => {
  return (
    <ThemeProvider theme={theme}>
      <CssBaseline />
      <AuthProvider>
        <ModelProvider>
          <Router>
            <Routes>
              {/* 公共路由 */}
              <Route path="/login" element={<Login />} />
              <Route path="/register" element={<Register />} />

              {/* 受保护路由 */}
              <Route
                path="/"
                element={
                  <ProtectedRoute>
                    <Dashboard />
                  </ProtectedRoute>
                }
              />
              {/* 模型管理路由 */}
              <Route
                path="/models"
                element={
                  <ProtectedRoute>
                    <ModelList />
                  </ProtectedRoute>
                }
              />
              <Route
                path="/models/create"
                element={
                  <ProtectedRoute>
                    <ModelCreate />
                  </ProtectedRoute>
                }
              />
              <Route
                path="/models/edit/:id"
                element={
                  <ProtectedRoute>
                    <ModelEdit />
                  </ProtectedRoute>
                }
              />
              {/* 模型预览路由 */}
              <Route
                path="/models/view/:id"
                element={
                  <ProtectedRoute>
                    <ModelView />
                  </ProtectedRoute>
                }
              />
              {/* 几何体模型路由 */}
              <Route
                path="/models/geometry/create"
                element={
                  <ProtectedRoute>
                    <GeometryModelCreate />
                  </ProtectedRoute>
                }
              />
              <Route
                path="/models/geometry/edit/:id"
                element={
                  <ProtectedRoute>
                    <GeometryModelEdit />
                  </ProtectedRoute>
                }
              />
              {/* 模型文件管理路由 */}
              <Route
                path="/models/:id/files"
                element={
                  <ProtectedRoute>
                    <ModelFiles />
                  </ProtectedRoute>
                }
              />
              {/* 材料库路由 */}
              <Route
                path="/material-library"
                element={
                  <ProtectedRoute>
                    <MaterialLibrary />
                  </ProtectedRoute>
                }
              />

              {/* 材料库路由（别名：textures） */}
              <Route
                path="/textures"
                element={
                  <ProtectedRoute>
                    <MaterialLibrary />
                  </ProtectedRoute>
                }
              />

              {/* 默认重定向到首页 */}
              <Route path="*" element={<Navigate to="/" replace />} />
            </Routes>
          </Router>
        </ModelProvider>
      </AuthProvider>
    </ThemeProvider>
  );
};

export default App;
